package com.scau.action;

import java.util.List;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;

import com.scau.service.ISerService;
import com.scau.vo.Service;
import com.scau.vo.ServiceCategory;

/**
 * @author Jinglun
 * @create 2017年3月17日
 */
public class ServiceAction extends SuperAction {
	private static final long serialVersionUID = 1L;
	public static Logger log = Logger.getLogger(AdminAction.class);
	private String result;// 属性result，用于异步传输数据交互
	
	private ISerService serService;
	private Service service;
	
	// 添加具体服务
	public String saveService() {
		Service service = getService();
		System.out.println("添加的服务："+service.getName());
		// 前端传数据时，将serCateId放在serCateName属性里
		System.out.println("serCateId:"+service.getSerCateName());
			
		if (serService.saveService(service)) {
			List<Service> serList = serService.listService();
			JSONArray jsonArray = JSONArray.fromObject(serList.toString());
			result = jsonArray.toString();
			return "success";
		} else
			return "error";
	}
	
	// 查看所有服务
	public String showService() {
		List<Service> serviceList = serService.listService();

		// 使用json方式返回值,toString必须
		JSONArray jsonArray = JSONArray.fromObject(serviceList.toString());
		result = jsonArray.toString();
		System.out.println("json的result："+result);
		return "success";
	}
	
	// 删除服务
	public String deleteService() {
		Service service = getService();
	
		if (serService.deleteService(service.getId())) {
			List<Service> serList = serService.listService();
			JSONArray jsonArray = JSONArray.fromObject(serList.toString());
			result = jsonArray.toString();
			return "success";
		} else
			return "error";
	}
	
	// 更新服务
	public String updateService(){
		Service ser = getService();
		
		if(serService.updateService(ser)){
			List<Service> serList = serService.listService();
			JSONArray jsonArray = JSONArray.fromObject(serList.toString());
			result = jsonArray.toString();
			return "success";
		}
		else return "error";
	}
	
	public String getServiceById(){
		Service s = getService();
		Service ser = serService.getServiceById(s.getId());
		if(ser != null){
			JSONArray jsonArray = JSONArray.fromObject(ser.toString());
			result = jsonArray.toString();
			return "success";
		} else return "error";
	}
	
	
	
	// get,set方法
	public Service getService() {
		return service;
	}
	public ISerService getSerService() {
		return serService;
	}

	public void setSerService(ISerService serService) {
		this.serService = serService;
	}

	public void setService(Service service) {
		this.service = service;
	}

	public String getResult() {
		return result;
	}


	public void setResult(String result) {
		this.result = result;
	}
	
	
}
